﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using SqlserverHelper;
using System.Threading;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using OJ;
using DLLmySQL;


public partial class getDataPSI : System.Web.UI.Page
{
    public string strCnn = ConfigurationSettings.AppSettings["DataBaseString"];

    protected void Page_Load(object sender, EventArgs e)
    {
        string customer_code = "";
        string shop_code = "";
        string biz_type = "";
        string prd_code = "";
        if (Request.Form["customer_code"] != null && Request.Form["customer_code"].ToString().Trim() != "")
        {
            customer_code = Request.Form["customer_code"].ToString();
        }

        if (Request.Form["shop_code"] != null && Request.Form["shop_code"].ToString().Trim() != "")
        {
            shop_code = Request.Form["shop_code"].ToString();
        }

        if (Request.Form["biz_type"] != null && Request.Form["biz_type"].ToString().Trim() != "")
        {
            biz_type = Request.Form["biz_type"].ToString();
        }
        if (Request.Form["prd_code"] != null && Request.Form["prd_code"].ToString().Trim() != "")
        {
            prd_code = Request.Form["prd_code"].ToString();
        }


        Define.retInfo ret = null;

        if (biz_type == "" || prd_code == "" || customer_code == "")
        {
            ret = new Define.retInfo();
            ret.code = 40001;
            ret.msg = "缺少所需参数";

            Response.Write(Ojson.ObjectToJson(ret));
            return;
        }

        string stResult = null;

        if (biz_type == "1") 
        {
            stResult = f_getJH(customer_code, prd_code);
        }
        else if (biz_type == "2") 
        {
            stResult = f_getXS(customer_code, prd_code);
        }
    
        Response.Write(stResult);
    }



    //进货表
    public string f_getJH(String customer_code, String prd_code)
    {
        Define.retJHList retJH = new Define.retJHList();

        try
        {
            string strQuery = "select id,ymd,seller_code,seller_name,purchaser_code,purchaser_name,prd_code,prd_name,prd_spec,prd_batch,prd_qty,prd_unit,prd_price,total,mfrs,pzwh,made_time,maker,updater_time,updater from dsnjh where purchaser_code =  '" + customer_code.Trim() + "' and prd_code = '" + prd_code.Trim() + "'";
            DataTable DT = CmySQL.GetDataTbale(strCnn, strQuery);

            if (DT != null && DT.Rows.Count > 0)
            {
                List<Define.JHList> L = new List<Define.JHList>();
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    Define.JHList r = new Define.JHList();

                    r.id = DT.Rows[i][0].ToString().Trim();
                    r.ymd = DT.Rows[i][1].ToString().Trim();
                    r.seller_code = DT.Rows[i][2].ToString().Trim();
                    r.seller_name = DT.Rows[i][3].ToString().Trim();
                    r.purchaser_code = DT.Rows[i][4].ToString().Trim();
                    r.purchaser_name = DT.Rows[i][5].ToString().Trim();
                    r.prd_code = DT.Rows[i][6].ToString().Trim();
                    r.prd_name = DT.Rows[i][7].ToString().Trim();
                    r.prd_spec = DT.Rows[i][8].ToString().Trim();
                    r.prd_batch = DT.Rows[i][9].ToString().Trim();
                    r.prd_qty = DT.Rows[i][10].ToString().Trim();
                    r.prd_unit = DT.Rows[i][11].ToString().Trim();
                    r.prd_price = DT.Rows[i][12].ToString().Trim();
                    r.total = DT.Rows[i][13].ToString().Trim();
                    r.mfrs = DT.Rows[i][14].ToString().Trim();
                    r.pzwh = DT.Rows[i][15].ToString().Trim();
                    r.made_time = DT.Rows[i][16].ToString().Trim();
                    r.maker = DT.Rows[i][17].ToString().Trim();
                    r.updater_time = DT.Rows[i][18].ToString().Trim();
                    r.updater = DT.Rows[i][19].ToString().Trim();
                    L.Add(r);
                }
                retJH.code = 200;
                retJH.msg = "success";
                retJH.data = L;
            }
            else
            {
                retJH.code = 200;
                retJH.msg = "无数据!";
                retJH.data = null;
            }
        }
        catch
        {
            retJH.code = 20000;
            retJH.msg = "服务不可用";
            retJH.data = null;
        }

        return Ojson.ObjectToJson(retJH);
    }


    //销售表
    public string f_getXS(String customer_code, String prd_code)
    {
        Define.retXSList retXS = new Define.retXSList();

        try
        {
            string strQuery = "select id,ymd,seller_code,seller_name,purchaser_code,purchaser_name,prd_code,prd_name,prd_spec,prd_batch,prd_qty,prd_unit,prd_price,total,mfrs,pzwh,sell_type,delivery_address,made_time,maker,updater_time,updater from dsnxs where seller_code =  '" + customer_code.Trim() + "' and prd_code = '" + prd_code.Trim() + "'";
            DataTable DT = CmySQL.GetDataTbale(strCnn, strQuery);

            if (DT != null && DT.Rows.Count > 0)
            {
                List<Define.XSList> L = new List<Define.XSList>();
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    Define.XSList r = new Define.XSList();

                    r.id = DT.Rows[i][0].ToString().Trim();
                    r.ymd = DT.Rows[i][1].ToString().Trim();
                    r.seller_code = DT.Rows[i][2].ToString().Trim();
                    r.seller_name = DT.Rows[i][3].ToString().Trim();
                    r.purchaser_code = DT.Rows[i][4].ToString().Trim();
                    r.purchaser_name = DT.Rows[i][5].ToString().Trim();
                    r.prd_code = DT.Rows[i][6].ToString().Trim();
                    r.prd_name = DT.Rows[i][7].ToString().Trim();
                    r.prd_spec = DT.Rows[i][8].ToString().Trim();
                    r.prd_batch = DT.Rows[i][9].ToString().Trim();
                    r.prd_qty = DT.Rows[i][10].ToString().Trim();
                    r.prd_unit = DT.Rows[i][11].ToString().Trim();
                    r.prd_price = DT.Rows[i][12].ToString().Trim();
                    r.total = DT.Rows[i][13].ToString().Trim();
                    r.mfrs = DT.Rows[i][14].ToString().Trim();
                    r.pzwh = DT.Rows[i][15].ToString().Trim();
                    r.sell_type = DT.Rows[i][16].ToString().Trim();
                    r.delivery_address = DT.Rows[i][17].ToString().Trim();
                    r.made_time = DT.Rows[i][18].ToString().Trim();
                    r.maker = DT.Rows[i][19].ToString().Trim();
                    r.updater_time = DT.Rows[i][20].ToString().Trim();
                    r.updater = DT.Rows[i][21].ToString().Trim();
                    L.Add(r);
                }
                retXS.code = 200;
                retXS.msg = "success";
                retXS.data = L;
            }
            else
            {
                retXS.code = 200;
                retXS.msg = "无数据!";
                retXS.data = null;
            }
        }
        catch
        {
            retXS.code = 20000;
            retXS.msg = "服务不可用";
            retXS.data = null;
        }

        return Ojson.ObjectToJson(retXS);
    }
}